package com.fyh.mapper;

import com.fyh.entity.chat.ChatPageProp;
import com.fyh.entity.chat.ChatRecord;
import com.fyh.entity.chat.InformRecord;
import com.fyh.entity.chat.SimpleContact;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ChatMapper {

    /**
     * 获取用户联系人Id列表
     * @param userId
     */
    @Select("select contact_user_id from chat_contact where user_id = #{userId}")
    List<Long> getUserContactIdList(Long userId);

    /**
     * 获取用户联系人列表
     * @param userId
     */
    @Select("select contact_user_id, contact_name from chat_contact where user_id = #{userId}")
    List<SimpleContact> getSimpleContactList(Long userId);

    /**
     * 获取联系人未读消息数量
     * @param senderId
     * @param receiverId
     */
    @Select("select count(*) from chat_record where sender_id = #{senderId} and receiver_id = #{receiverId} and status = 0")
    Integer getUnreadMessageCount(@Param("senderId") Long senderId, @Param("receiverId") Long receiverId);

    /**
     * 记录聊天记录
     * @param chatRecord
     */
    @Insert("insert into chat_record (sender_id, receiver_id, message, send_time) values (#{senderId}, #{receiverId}, #{message}, #{sendTime})")
    void insertChatRecord(ChatRecord chatRecord);

    /**
     * 用户点击联系人打开聊天窗口后，更新聊天记录状态为已读
     * @param chatRecord
     */
    @Update("update chat_record set status = 1 where sender_id = #{senderId} and receiver_id = #{receiverId} and status = 0")
    void updateChatRecordStatus(ChatRecord chatRecord);

    /**
     * 获取聊天记录列表
     * @param chatPageProp
     */
    List<ChatRecord> getChatRecordList(ChatPageProp chatPageProp);

    /**
     * 添加联系人
     * @param userId
     * @param userName
     * @param contactId
     * @param contactName
     * @param now
     */
    @Insert("insert ignore into chat_contact (user_id, contact_user_id, contact_name, update_time, create_time) values " +
            "(#{userId}, #{contactId}, #{contactName}, #{now}, #{now}), " +
            "(#{contactId}, #{userId}, #{userName}, #{now}, #{now})")
    void addChatContact(@Param("userId") Long userId,
                        @Param("userName") String userName,
                        @Param("contactId") Long contactId,
                        @Param("contactName") String contactName,
                        @Param("now") LocalDateTime now);

    /**
     * 获取用户是否有未读消息
     * @param userId
     */
    @Select("select exists(select * from chat_record where receiver_id = #{userId} and status = 0)")
    Boolean getUnreadMessage(Long userId);


    /**
     * 更新联系人备注
     * @param userId
     * @param contactId
     * @param contactName
     */
    @Update("update chat_contact set contact_name = #{contactName} where user_id = #{userId} and contact_user_id = #{contactUserId}")
    void updateContactName(@Param("userId") Long userId, @Param("contactUserId") Long contactId, @Param("contactName") String contactName);

    /**
     * 获取通知消息记录
     * @param userId
     */
    @Select("select receiver_id, message, receive_time from inform_record where receiver_id = #{userId} order by id desc")
    List<InformRecord> getUserInformRecord(Long userId);

    /**
     * 获取未读通知数量
     * @param userId
     */
    @Select("select count(*) from inform_record where receiver_id = #{userId} and status = 0")
    Integer getUnreadInform(Long userId);

    /**
     * 插入通知消息记录
     * @param informRecord
     */
    @Insert("insert into inform_record (receiver_id, message, receive_time) values (#{receiverId}, #{message}, #{receiveTime})")
    void insertInformRecord(InformRecord informRecord);

    /**
     * 更新通知消息状态为已读
     * @param userId
     */
    @Update("update inform_record set status = 1 where receiver_id = #{userId}")
    void updateInformStatus(Long userId);
}
